package com.zmd.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class Logger {

	@Pointcut("execution(public int com.zmd.aop.Utils.add(int, int))")
	private void fun() {
		System.out.println(Logger.class.getName() + " " + "fun");
	}
	
	@Before("fun()")
	public void before() {
		System.out.println(Logger.class.getName() + " " + "before");
	}
	
	@After("fun()")
	public void after() {
		System.out.println(Logger.class.getName() + " " + "after");
	}
	
	@AfterReturning(pointcut = "fun()", returning = "ret")
	public void afterReturning(Object ret) {
		System.out.println(Logger.class.getName() + " " + "afterReturning" + ret.toString());
	}
	
	@AfterThrowing(pointcut = "fun()", throwing = "exception")
	public void afterThrowing(Exception exception) {
		System.out.println(Logger.class.getName() + " " + "afterThrowing" + exception.toString());
	}
	
	
	@Around("fun()")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println(Logger.class.getName() + " " + "around0");
		Object retObject = null;
		try {
			retObject = joinPoint.proceed();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(Logger.class.getName() + " " + "around1");
		}
		System.out.println(Logger.class.getName() + " " + "around2");
		return retObject;
	}
}
